package com.apestech.framework.run.datamapper;

import com.apestech.formwork.xml.core.var.NodeVarMapping;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONObject;

/**
 * 单个数据Ｍapping的数据模型
 *
 * @author hup
 * @create 2018-03-30-16:42
 */
public class SingleDataMappingModule {

    private String field_id=null;
    private String target=null;
    private String field_name=null;

    private String field_type=null;
    private String allow_null=null;
    private String default_value=null;
    private String field_check=null;

    private String value=null;
    private String tableName=null;
    private String express=null;
    private Math math=null;
    private JSONObject json=null;

    public void setField(String field_id, String target, String field_name,
                         String field_type, String allow_null, String default_value, String field_check,
                         String value, String tableName, String express, String math_id, int math_size){
        setField(field_id, target, field_name);
        setField_type(field_type);
        setAllow_null(allow_null);
        setDefault_value(default_value);
        setField_check(field_check);
        setValue(value);
        setTableName(tableName);
        setExpress(express);
        setMath(math_id, math_size);
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public void setField(String field_id, String target, String field_name){
        this.field_id=field_id;
        if(target==null){
            this.target=field_id;
        } else {
            this.target=target;
        }
        if(field_name==null){
            this.field_name=field_id;
        } else {
            this.field_name=field_name;
        }
    }

    public void setField_type(String field_type) {
        this.field_type = field_type;
    }

    public void setAllow_null(String allow_null) {
        this.allow_null = allow_null;
    }

    public void setDefault_value(String default_value) {
        this.default_value = default_value;
    }

    public void setField_check(String field_check) {
        this.field_check = field_check;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setExpress(String express) {
        this.express = express;
    }

    public void setMath(String id, int size) {
        if(this.math==null){
            this.math = new Math(id, size);
        }
    }

    public JSONObject getField(){
        if(this.field_id==null) return null;
        this.json=new JSONObject();
        JSONObject f=new JSONObject();
        if(this.value!=null){
            f.put(this.field_id,this.value);
        } else {
            f.put(NodeVarMapping.getTARGET(), this.target);
            f.put(NodeVarMapping.getFieldName(), this.field_name);
            f.put(NodeVarMapping.getFieldType(), this.field_type);
            f.put(NodeVarMapping.getAllowNull(), this.allow_null);
            f.put(NodeVarMapping.getDefaultValue(), this.default_value);
            f.put(NodeVarMapping.getFieldCheck(), this.field_check);
            f.put(NodeVarMapping.getEXPRESS(), this.express);
            f.put(NodeVarMapping.getMATH(), this.math);
        }
        this.json.put(this.field_id, f);
        return this.json;
    }

    public JSONObject getJSON(){
        if(this.json==null){
            getField();
        }
        return this.json;
    }

    public String getField_id() {
        return field_id;
    }

    public String getTarget() {
        return target;
    }

    public String getField_type() {
        return field_type;
    }

    public String getAllow_null() {
        return allow_null;
    }

    public String getDefault_value() {
        return default_value;
    }

    public String getField_check() {
        return field_check;
    }

    public String getField_name() {
        return field_name;
    }

    public String getValue() {
        return value;
    }

    public String getExpress() {
        return express;
    }

    public Math getMath() {
        return math;
    }

    public String toString(){
        try {
            JSONObject map=getJSON();
            if(map==null) return "";
            return JSON.toJSONString(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}


class Math{
    private String id;
    private int size;

    public Math(String id, int size){
        this.id=id;
        this.size=size;
    }

    public String getId() {
        return id;
    }

    public int getSize() {
        return size;
    }

}
